<?php


namespace app\api\model;


use think\Model;

class TrainRecord extends Model
{
    protected $autoWriteTimestamp = true;

    protected $pk = 'record_id';

    public static function getTrainRecord($childId)
    {
        return self::where('child_id', $childId)
            ->where('train_time', strtotime(date('Y-m-d')))
            ->find();
    }

    public static function createTrainRecord($userId, $trainId, $childId)
    {
        return self::create([
            'user_id' => $userId,
            'child_id' => $childId,
            'train_id' => $trainId,
            'train_time' => strtotime(date('Y-m-d'))
        ]);
    }

    public static function getTrainGroupList($userId)
    {
        return self::where('user_id', $userId)
            ->field('train_id')
            ->group('train_id,train_time')
            ->order('train_time desc')
            ->select()
            ->toArray();
    }

    public static function countTrainGroupList($userId)
    {
        return self::where('user_id', $userId)
            ->field('train_id')
            ->group('train_id,train_time')
            ->count();
    }
}